<template>
<form @submit="submitFun">
	<image mode='widthFix' class='app_login_bg' src='https://image1.sansancloud.com/xianhua/2019_11/26/9/32/2_137.jpg?x-oss-process=style/preview_120'></image>
	<view class='container'>
	  <view class='app_logo_block'>
	    <image mode='aspectFit' class='app_logo_img' :src='setting.platformSetting.logo&&setting.platformSetting.logo!="undefined"?setting.platformSetting.logo:"https://image1.sansancloud.com/sansancloud/2018_7/14/11/21/37_175.jpg?x-oss-process=style/preview_120"'></image>
	  </view>
	  <view class='app_input_block'>
	    <view class='app_input_item'>
	      <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_966.jpg?x-oss-process=style/preview_120'></image>
	      <input placeholder='请输入您的手机号' name="telno" style='width:90%;'></input>
	    </view>
        <block v-if="verifyStatus" :animation="verifyAnimation">
            <view class='app_input_item'>
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/28/16/24/17_7.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请输入验证码' name="verifyCode" style='width:90%;'></input>
            </view>
            <view class='app_input_item'>
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/28/16/18/51_470.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请输入昵称' name="nickname" style='width:90%;'></input>
            </view>
            <view class='app_input_item'>
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/28/16/20/53_33.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请输入推广码' name="belongChannelCode" style='width:90%;'></input>
            </view>
            <view class='app_input_item'>
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_968.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请输入密码' name="password" password style='width:90%;'></input>
            </view>
            <view class='app_input_item'>
              <image class='app_input_item_icon' src='https://image1.sansancloud.com/xianhua/2019_11/26/10/22/26_968.jpg?x-oss-process=style/preview_120'></image>
              <input placeholder='请再次输入密码' password name="password2" style='width:90%;'></input>
            </view>
        </block>
	  </view>
	  <view class='app_botton_block'>
	     <button class='app_botton_item register' :loading="registStatus" v-if="verifyStatus" :data-type="registType" form-type="submit" :style="{background:setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5',border:'1rpx solid'+(setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5')}">立即注册</button>
	     <button class='app_botton_item register' :loading="verifyLoading" :data-type="verifyType" form-type="submit" :style="{background:setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5',border:'1rpx solid'+(setting.platformSetting?setting.platformSetting.defaultColor:'#2269d5')}">
             <text v-if="!verifyLoading&&waitingTime==0">获取验证码</text>
             <text v-else>重新发送({{waitingTime}})</text>
         </button>
		 <view class="agreement_block">
			 <text class='agreementTip'>注册表示您已同意</text>
			 <text class='agreementName' @click='tolinkUrl' >《注册协议》</text>
		 </view>
	  </view>
	</view>
</form>
</template>

<script>
import { socketFun } from "../../wxcomponents/public/json2Form";
export default {
  data() {
    return {
      loginData:{},
	  loginState:false,
	  setting:{},
      verifyStatus:false,
      verifyLoading:false,
      registStatus:false,
      verifyType:'verifyType',
      waitingTime:0,
      registType:'registType',
      verifyAnimation:null,
    };
  },
  props: {},

  /*onload*/
  onLoad: function (options) {
    // wx.hideTabBar({})
    console.warn("======onLoad:options======", options, getApp().globalData.setting);
	this.setData({setting:getApp().globalData.setting})
  },
  onReady: function () {
    
  },
  onShow: function () {
    let that=this;
	that.loginState=false;
  },
  /* 分享 app.js862行*/
  onShareAppMessage: function () {
    console.log(getApp().globalData.miniIndexPage);
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  onPullDownRefresh: function () {
    let that = this; // 下拉刷新的时候首先判断存不存在tab
    console.log("下拉");
    that.refreshPage();
    wx.stopPullDownRefresh();
  },
  methods: {
	tolinkUrl:function(){
		console.log("===tolinkUrl===")
		let linkUrl='custom_page_regist_agreement.html';
		console.log("===linkUrl===",linkUrl)
		getApp().globalData.linkEvent(linkUrl);
	},
    submitFun:function(e){
        let that=this;
        let type=e.detail.target.dataset.type;
        console.log("=====submitFun====",e,type)
        let params={}
        if(type=='verifyType'){
            params={
               telno:e.detail.value.telno,
            };
            that.setData({verifyLoading:true})
            that.getVerifyCodeFun(params)
        }else{
            params={
               username:e.detail.value.telno,
               verifyCode:e.detail.value.verifyCode,
               password:e.detail.value.password,
               password2:e.detail.value.password2,
               belongChannelCode:e.detail.value.belongChannelCode,
               nickname:e.detail.value.nickname,
            };
            that.registFun(params);
        }
    },
    registFun:function(params){
        console.log("===registFun====",params)
        let that=this;
        for(let attr in params){
            if(!params[attr]&&attr=='verifyCode'){
                getApp().globalData.showToastFun("请输入验证码")
                return
            }else if(!params[attr]&&attr=='nickname'){
                getApp().globalData.showToastFun("请输入昵称")
                return
            }else if(!params[attr]&&attr=='password'){
                getApp().globalData.showToastFun("请输入密码")
                return
            }else if(!params[attr]&&attr=='password2'){
                getApp().globalData.showToastFun("请再次输入密码")
                return
            }else if(params[attr]&&attr=='password2'&&params[attr]!=params['password']){
                getApp().globalData.showToastFun("密码不一致")
                return
            }else if(params[attr]&&attr=='password'&&params[attr]!=params['password2']){
                getApp().globalData.showToastFun("密码不一致")
                return
            }
        };
        that.setData({registStatus:true})
        let customIndex = getApp().globalData.AddClientUrl("/regist.html", params, 'post', '1'); //拿custom_page
        console.log("customIndex",customIndex)
        uni.request({
            url: customIndex.url,
            data: customIndex.params,
            method: 'POST',
            header: getApp().globalData.headerPost,
            success: function (res) {
              console.log("====== res.data=========", res.data);
              wx.hideLoading();
              that.setData({registStatus:false})
              if (!res.data.errcode || res.data.errcode == '0') {
                  getApp().globalData.showToastFun("注册成功！",1000)
                  setTimeout(function() {
                      getApp().globalData.showToastLoading('正在登入中...');
                      let loginData={
                           username:params.username,
                           password:params.password,
                       }
                      that.loginFun(loginData)
                  }, 1000);
              } else {
                let text=res.data.errMsg||'注册失败，点击【确定】重新注册';
                wx.showModal({
                    title: '提示',
                    content: text,
                    success: function (res) {
                      if (res.confirm) {
                      } else if (res.cancel) {
                        // getApp().globalData.toIndex();
                      }
                    }
                });
              }
            },
            fail: function (res) {
              console.log(res);
              wx.hideLoading();
              wx.showModal({
                title: '提示',
                content: '注册失败，点击【确定】重新注册',
                success: function (res) {
                  if (res.confirm) {
                    // that.getParac();
                  } else if (res.cancel) {
                    // getApp().globalData.toIndex();
                  }
                }
              });
            }
        });
    },
	getVerifyCodeFun:function(params){
	  let that=this;
      console.log("===getVerifyCodeFun====",params)
      let animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease',
      })
      animation.opacity(1).step();
      if(!params.telno){
        wx.showModal({
            title: '提示',
            content: '请您填写手机号！',
            success: function (res) {
              if (res.confirm) {
              } else if (res.cancel) {
              }
            }
        });
        return
      }
	  let customIndex = getApp().globalData.AddClientUrl("/get_verify_code.html", params, 'post', '1'); //拿custom_page
	  console.log("customIndex",customIndex)
	  uni.request({
		url: customIndex.url,
		data: customIndex.params,
		method: 'POST',
		header: getApp().globalData.headerPost,
		success: function (res) {
		  console.log("====== res.data=========", res.data);
		  wx.hideLoading();
		  if (!res.data.errcode || res.data.errcode == '0') {
             that.waitingTime=10
             let clock =setInterval(function () {
             that.waitingTime--;
             if(that.waitingTime<=0){
                that.waitingTime=0;
                clearInterval(clock);
              }
            },1000)
			that.loginState=true;
			that.setData({
              verifyAnimation:animation,
			  loginData:res.data,
              verifyStatus:true,
              verifyLoading:false
			});
             getApp().globalData.showToastFun("手机验证码已发送，请查看手机！",2000,"none")
		  } else {
            that.setData({verifyLoading:false})
			that.loginState=false;
            let text=res.data.errMsg||'获取验证码失败';
			wx.showModal({
				title: '提示',
				content: text,
				success: function (res) {
				  if (res.confirm) {
				  } else if (res.cancel) {
					// getApp().globalData.toIndex();
				  }
				}
			});
		  }
		},
		fail: function (res) {
		  console.log(res);
		  wx.hideLoading();
		  that.loginState=false;
		  wx.showModal({
			title: '提示',
			content: '获取失败，点击【确定】重新获取',
			success: function (res) {
			  if (res.confirm) {
				// that.getParac();
			  } else if (res.cancel) {
				// getApp().globalData.toIndex();
			  }
			}
		  });
		}
	  });
	},
    loginFun:function(params){
	  let that=this;
	  console.log("====loginFun====",params)
	  console.log("====params====",params)
	  let customIndex = getApp().globalData.AddClientUrl("/login.html?jsonOnly=1", params, 'post', '1'); //拿custom_page
	  console.log("customIndex",customIndex)
	  uni.request({
		url: customIndex.url,
		data: customIndex.params,
		method: 'POST',
		header: getApp().globalData.headerPost,
		success: function (res) {
		  console.log("====== res.data=========", res.data);
		  wx.hideLoading();
		  if (!res.data.errcode || res.data.errcode == '0') {
			getApp().globalData.loginUser=res.data.relateObj
			that.setData({
			  loginData:res.data
			});
            socketFun.socketConnect()
			getApp().globalData.toIndex();
		  } else {
			that.loginState=false;
			wx.showModal({
				title: '提示',
				content: '登录失败，点击【确定】重新登录',
				success: function (res) {
				  if (res.confirm) {
                     getApp().globalData.linkEvent('app_login_page.html');
				  } else if (res.cancel) {
					getApp().globalData.toIndex();
				  }
				}
			});
		  }
		},
		fail: function (res) {
		  console.log(res);
		  wx.hideLoading();
		  that.loginState=false;
		  wx.showModal({
			title: '提示',
			content: '登录失败，点击【确定】重新登录',
			success: function (res) {
			  if (res.confirm) {
				getApp().globalData.linkEvent('app_login_page.html');
			  } else if (res.cancel) {
				// getApp().globalData.toIndex();
			  }
			}
		  });
		}
	  });
	},
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>